package clearjerk.pic.model;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.awt.image.RasterFormatException;

public class ImageMod {
	private BufferedImage image;
	private Color[][] pixels;
	private int width;
	private int height;
	public ImageMod(){
		image = null;
	}
	public ImageMod(BufferedImage im){
		image= im;
		width = image.getWidth();
		height = image.getHeight();
		if(width%2 != 0)
			width=width-1;
		if(height%2 !=0)
			height = height-1;
		pixels = new Color[width][height];
		for(int x = 0; x< width; x++)
			for(int y = 0; y< height; y++){
				pixels[x][y]=new Color(image.getRGB(x,y));
			}
	}
	public BufferedImage makeAverages(int sideLength) {
		if(sideLength > width)
			return image;
		BufferedImage img = new BufferedImage(sideLength, sideLength, BufferedImage.TYPE_INT_RGB);
		Graphics2D g = img.createGraphics();
		for(int i = 0; i < sideLength; i++)
			for(int j = 0; j < sideLength; j++) {
				int pixelWidth = width/sideLength;
				int pixelHeight = height/sideLength;
				int[] totals = new int[3];
				int count = 0;
				for(int x = i*pixelWidth; x<i*pixelWidth+pixelWidth; x++)
					for(int y = j*pixelHeight; y<j*pixelHeight+pixelHeight; y++) {
						count++;
						Color temp = pixels[x][y];
						totals[0] += temp.getRed();
						totals[1] += temp.getGreen();
						totals[2] += temp.getBlue();
					}
				Color average = new Color(totals[0]/count, totals[1]/count, totals[2]/count);
				g.setColor(average);
				g.drawRect(i,j,1,1);			
			}
				
		return img;

	}
    public BufferedImage pixelateImage(int pixelSize) {
        if (pixelSize == 0) {
                pixelSize = 1;
        }

        final int width = image.getWidth();
        final int height = image.getHeight();
        final BufferedImage out = new BufferedImage(width, height, image.getType());

        int rasterWidth = width / pixelSize;
        if (width % pixelSize > 0) {
                rasterWidth++;
        }
        int rasterHeight = height / pixelSize;
        if (height % pixelSize > 0) {
                rasterHeight++;
        }

        int alpha, r, g, b, nextX, nextY, rgb, posX, posY;
        for (int x = 0; x < rasterWidth; x++) {
                for (int y = 0; y < rasterHeight; y++) {
                        alpha = 0;
                        r = 0;
                        g = 0;
                        b = 0;

                        nextX = Math.min(pixelSize, width - (x * pixelSize));
                        nextY = Math.min(pixelSize, height - (y * pixelSize));

                        for (posX = x * pixelSize; posX < x * pixelSize + nextX; posX++) {
                                for (posY = y * pixelSize; posY < y * pixelSize + nextY; posY++) {
                                        try {
                                                rgb = image.getRGB(posX, posY);
                                        } catch (final RasterFormatException e) {
                                                rgb = 0;
                                        }

                                        // get the rgb-values
                                        alpha = alpha + ((rgb & 0xff000000) >> 24);
                                        r = r + ((rgb & 0x00ff0000) >> 16);
                                        g = g + ((rgb & 0x0000ff00) >> 8);
                                        b = b + ((rgb & 0x000000ff));
                                }
                        }

                        // figure out medium color
                        alpha = alpha / (nextX * nextY);
                        r = r / (nextX * nextY);
                        g = g / (nextX * nextY);
                        b = b / (nextX * nextY);

                        // make new color
                        rgb = ((alpha & 0xff) << 24) | ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff);

                        // write it for all pixels
                        for (posX = x * pixelSize; posX < x * pixelSize + nextX; posX++) {
                                for (posY = y * pixelSize; posY < y * pixelSize + nextY; posY++) {
                                        try {
                                                out.setRGB(posX, posY, rgb);
                                        } catch (final RasterFormatException e) {
                                        }
                                }
                        }
                }
        }

        return out;
    }
}



